package com.my.lucky.dto;

import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 股票分析结果DTO
 * 用于展示股票选出时的价格和后续几天的最高价分析
 */
@Data
public class StockAnalysisResult {

    /**
     * 股票代码
     */
    private String stockCode;

    /**
     * 股票名称
     */
    private String stockName;

    private Double searchangePercent;//查询出来涨幅
    /**
     * 选出时间
     */
    private LocalDateTime selectedTime;

    /**
     * 选出时的价格
     */
    private Double selectedPrice;

    /**
     * 当日价格（选出当天的收盘价）
     */
    private Double firstDayPrice;

    /**
     * 当日涨幅（选出时价格相对于当日收盘价的涨幅）
     */
    private Double firstDayGainPercent;

    /**
     * 第二天的最高价
     */
    private Double secondDayHighPrice;

    /**
     * 第三天的最高价
     */
    private Double thirdDayHighPrice;

    /**
     * 第二天涨幅（相对于选出时价格）
     */
    private Double secondDayGainPercent;

    /**
     * 第三天涨幅（相对于选出时价格）
     */
    private Double thirdDayGainPercent;

    /**
     * 第二天涨幅（相对于当日价格）
     */
    private Double secondDayGainPercentFromFirstDay;

    /**
     * 第三天涨幅（相对于当日价格）
     */
    private Double thirdDayGainPercentFromFirstDay;

    /**
     * 最大涨幅（第二天和第三天中的最大涨幅）
     */
    private Double maxGainPercent;

    /**
     * 选出时的概念板块
     */
    private String concept;

    /**
     * 选出时的概念板块
     */
    private String allconcept;

    /**
     * 选出时的类型（1-重点关注，2-新增量，3-洗盘，4-连续）
     */
    private Integer type;

    /**
     * 关键词ID
     */
    private Long keywordId;

    /**
     * 关键词内容
     */
    private String keyword;

    /**
     * 在时间段内的出现次数
     */
    private Integer occurrenceCount;


    /**
     * 排名位置
     */
    private Long rankPosition;

    /**
     * 老的排名位置
     */
    private Long oldRankPosition;

    private BigDecimal sevenDaysMainFlow;
    /**
     * 主力资金流向
     */
    private BigDecimal mainCapitalFlow;

    /**
     * 资金流向
     */
    private BigDecimal capitalFlow;


    public StockAnalysisResult() {
    }

    public StockAnalysisResult(String stockCode, String stockName, LocalDateTime selectedTime,
                               Double selectedPrice, Double searchangePercent, Long keywordId, String keyword, String concept,
                               String allconcept, Integer type, Long rankPosition, Long oldRankPosition, BigDecimal mainCapitalFlow, BigDecimal capitalFlow) {
        this.stockCode = stockCode;
        this.stockName = stockName;
        this.selectedTime = selectedTime;
        this.selectedPrice = selectedPrice;
        this.searchangePercent = searchangePercent;
        this.keywordId = keywordId;
        this.keyword = keyword;
        this.concept = concept;
        this.allconcept = allconcept;
        this.type = type;
        this.rankPosition = rankPosition;
        this.oldRankPosition = oldRankPosition;
        this.mainCapitalFlow = mainCapitalFlow;
        this.capitalFlow = capitalFlow;
    }

    public StockAnalysisResult(String stockCode, String stockName, LocalDateTime selectedTime,
                               Double selectedPrice, Double firstDayPrice, Long keywordId, String keyword, String concept, Integer type) {
        this.stockCode = stockCode;
        this.stockName = stockName;
        this.selectedTime = selectedTime;
        this.selectedPrice = selectedPrice;
        this.firstDayPrice = firstDayPrice;
        this.keywordId = keywordId;
        this.keyword = keyword;
        this.concept = concept;
        this.type = type;
    }

    /**
     * 计算涨幅百分比
     */
    public void calculateGainPercents() {
        // 计算当日涨幅（选出时价格相对于当日收盘价的涨幅）
        if (selectedPrice != null && firstDayPrice != null && selectedPrice > 0) {
            firstDayGainPercent = ((firstDayPrice - selectedPrice) / selectedPrice) * 100;
        }

        // 计算相对于选出时价格的涨幅
        if (selectedPrice != null && selectedPrice > 0) {
            if (secondDayHighPrice != null) {
                secondDayGainPercent = ((secondDayHighPrice - selectedPrice) / selectedPrice) * 100;
            }
            if (thirdDayHighPrice != null) {
                thirdDayGainPercent = ((thirdDayHighPrice - selectedPrice) / selectedPrice) * 100;
            }

            // 计算最大涨幅
            if (secondDayGainPercent != null && thirdDayGainPercent != null) {
                maxGainPercent = Math.max(secondDayGainPercent, thirdDayGainPercent);
            } else if (secondDayGainPercent != null) {
                maxGainPercent = secondDayGainPercent;
            } else if (thirdDayGainPercent != null) {
                maxGainPercent = thirdDayGainPercent;
            }
        }

        // 计算相对于当日价格的涨幅
        if (firstDayPrice != null && firstDayPrice > 0) {
            if (secondDayHighPrice != null) {
                secondDayGainPercentFromFirstDay = ((secondDayHighPrice - firstDayPrice) / firstDayPrice) * 100;
            }
            if (thirdDayHighPrice != null) {
                thirdDayGainPercentFromFirstDay = ((thirdDayHighPrice - firstDayPrice) / firstDayPrice) * 100;
            }
        }
    }

    /**
     * 获取类型描述
     */
    public String getTypeDescription() {
        if (type == null) return "未知";
        switch (type) {
            case 1:
                return "重点关注";
            case 2:
                return "新增量";
            case 3:
                return "洗盘";
            case 4:
                return "连续";
            default:
                return "普通";
        }
    }
} 